package com.meiqiongproperty.yiguanjia.fragment.home.health;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.WriteQuestionContentLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendQuestionSubmit;
import com.meiqiongproperty.yiguanjia.httpbean.healthy.SubmitQuestionContentBean;
import com.meiqiongproperty.yiguanjia.utils.Utils;

/**
 * author: Administrator
 * date: 2017/5/24.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.health
 */

public class WriteQuestionContentFragment extends BaseFragment {
    WriteQuestionContentLayoutBinding binding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.write_question_content_layout, container, false);
        binding.setClick(this);
        initView();
        return binding.getRoot();
    }

    public void initView() {
        binding.navigation.setTitle("填写提问内容");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        binding.etQuestionSubmitTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    binding.tvHintTitle.setVisibility(View.VISIBLE);
                } else {
                    binding.tvHintTitle.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        binding.etQuestionDescrition.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length()==0){
                    binding.tvContentNum.setText("0/200");
                    return;
                }
                binding.tvContentNum.setText(s.length()+"/200");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 点击提交
     */
    public void sumitQuestion(View view) {
        submitQuestionContent();
    }

    /**
     * 提交提问内容
     */
    public void submitQuestionContent() {
        SendQuestionSubmit send = new SendQuestionSubmit();
        send.setToken(Utils.getToken(getActivity()));
        send.setRoomID(Utils.getRoomID(getActivity()));
        send.setTitle(binding.etQuestionSubmitTitle.getText().toString());
        send.setContent(binding.etQuestionDescrition.getText().toString());

        ApiService.sumbitQuestionContent(getActivity(), new IHttpCallback<SubmitQuestionContentBean>() {
            @Override
            public void onNext(SubmitQuestionContentBean submitQuestionContentBean) {
                Toast.makeText(getActivity(), "提交成功", Toast.LENGTH_SHORT).show();
                getActivity().finish();
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(send));
    }
}
